var urlParse = function () {
    var search = window.location.search;
    if (!search) {
        return {};
    }
    var search_arr = search.replace(/\?/gi , '').split('&') , search_obj = {};
    search_arr.forEach(function (s) {
        s = s.split('=');
        search_obj[s[0]] = s[1];
    });
    return search_obj;
}
var ajax = {};
ajax.get = function (config) {
    var xhr = new XMLHttpRequest();
    if (!config.data) {
        config.data = {};
    }
    config.data.date = new Date().getTime();
    if (config.data) {
        var search = [];
        for (var i in config.data) {
            if (config.data[i] == undefined || config.data[i] == null)
                continue;
            search.push(i + "=" + config.data[i]);
        }
        config.url = config.url + "?" + search.join("&");
    }
    xhr.open('GET' , config.url , true);
    xhr.setRequestHeader("Accept" , "application/json, text/javascript, */*; q=0.01");
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState != 4)
            return;
        if (xhr.status == 200 || xhr.status == 304) {
            config.success && config.success(JSON.parse(xhr.responseText));
            return;
        }
        config.error && config.error(JSON.parse(xhr.responseText));
    };
}
ajax.post = function (config) {
    var xhr = new XMLHttpRequest();
    var query = "";
    if (config.data) {
        var search = [];
        for (var i in config.data) {
            if (config.data[i] == undefined || config.data[i] == null)
                continue;
            search.push(i + "=" + config.data[i]);
        }
        query = search.join("&");
    }
    xhr.open("POST" , config.url , true);
    xhr.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded;");
    xhr.setRequestHeader("Accept" , "application/json, text/javascript, */*; q=0.01");
    xhr.send(query);
    xhr.onreadystatechange = function () {
        if (xhr.readyState != 4)
            return;
        if (xhr.status == 200 || xhr.status == 304) {
            config.success && config.success(JSON.parse(xhr.responseText));
            return;
        }
        config.error && config.error(JSON.parse(xhr.responseText));
    };
}
var $ = function (selector) {
    return document.querySelector(selector);
}
var $$ = function (selector) {
    return document.querySelectorAll(selector);
}
HTMLElement.prototype.addClass = function (className) {
    if (this.className.indexOf(className) != -1)
        return;
    this.className = this.className + " " + className;
    return this;
}
HTMLElement.prototype.removeClass = function (className) {
    if (this.className.indexOf(className) == -1)
        return this;
    this.className = this.className.replace(className , '');
    return this;
}
HTMLElement.prototype.attr = function (key , value) {
    if (!key)
        return;
    if (!value) {
        return this.getAttribute(key);
    }
    this.setAttribute(key , value);
    return this;
}
HTMLElement.prototype.text = function (str) {
    if (str !== undefined) {
        this.innerText = str;
    } else {
        return this.innerText;
    }
}
HTMLElement.prototype.html = function (str) {
    if (str !== undefined) {
        this.innerHTML = str;
    } else {
        return this.innerHTML;
    }
}
HTMLElement.prototype.setVisible = function (b) {
    this.style.visibility = b ? 'visible' : 'hidden';
    return this;
}
HTMLElement.prototype.hasClass = function (cla) {
    return this.className.indexOf(cla) != -1;
}
HTMLElement.prototype.clearClass = function () {
    this.className = "";
    return this;
}
HTMLElement.prototype.tap = function (func) {
    var start_client_x , start_client_y , end_client_x , end_client_y;
    this.addEventListener('touchstart' , function (e) {
        var touch = e.changedTouches[0];
        start_client_x = touch.clientX;
        start_client_y = touch.clientY;
    });
    this.addEventListener('touchend' , function (e) {
        var touch = e.changedTouches[0];
        end_client_x = touch.clientX;
        end_client_y = touch.clientY;
        if (start_client_x != end_client_x || start_client_y != end_client_y)
            return;
        func(e);
    });
    return this;
}

HTMLElement.prototype.touchstart = function (func) {
    var funcBound = func.bind(this);
    this.addEventListener('touchstart' , function (e) {
        funcBound(e);
    });
}

HTMLElement.prototype.touchend = function (func) {
    var funcBound = func.bind(this);
    this.addEventListener('touchend' , function (e) {
        funcBound(e);
    });
}
HTMLElement.prototype.find = function (str) {
    return this.querySelector(str);
}
String.prototype.trim = function () {
    return this.replace(/^\s+|\s+$/gi , '');
}

